package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmNewsSensitiveMapper;
import com.heima.wemedia.service.WmNewsSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WmNewsSensitiveServiceImpl extends ServiceImpl<WmNewsSensitiveMapper, WmSensitive> implements WmNewsSensitiveService {

    /**
     * 查询所有敏感词
     * @return
     */
    @Override
    public ResponseResult listSensitiveWords(SensitiveDto sensitiveDto) {
        if(sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页查询
        IPage page = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());
        if(sensitiveDto.getName() != null){
            LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<WmSensitive>()
                    .like(WmSensitive::getSensitives, sensitiveDto.getName());
            page = page(page, queryWrapper);
        }else{
            page = page(page, null);
        }
        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;

    }

    @Override
    public ResponseResult addSensitiveWord(WmSensitive wmSensitive) {
        // 实现新增逻辑
        this.save(wmSensitive);
        return ResponseResult.okResult("敏感词添加成功");
    }

    @Override
    public ResponseResult deleteSensitiveWord(Integer id) {
        // 实现删除逻辑
        this.removeById(id);
        return ResponseResult.okResult("敏感词删除成功");
    }


    @Override
    public ResponseResult updateSensitiveWord(WmSensitive wmSensitive) {
        // 实现修改逻辑
        if (wmSensitive.getId() == null) {
            return ResponseResult.errorResult(400, "ID不能为空");
        }
        this.updateById(wmSensitive);
        return ResponseResult.okResult("敏感词修改成功");
    }

}
